//
//  WBRetweetedView.swift
//  WeiboTen
//
//  Created by ItHeiMa on 2017/7/9.
//  Copyright © 2017年 itHeima. All rights reserved.
//

import UIKit

class WBRetweetedView: UIView {
    
    var statusViewModel: WBStatusViewModel? {
        didSet {
            pictureView.statusViewModel = statusViewModel

            textLabel.text = statusViewModel?.statusModel.retweeted_status?.text
            
            //判断是否有配图视图， 调整配图视图的约束
            if let pic_urls = statusViewModel?.statusModel.retweeted_status?.pic_urls, pic_urls.count > 0 {
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(textLabel.snp.bottom).offset(10)
                    make.size.equalTo(statusViewModel!.pictureSize).priority(999)
                })
            } else {
                //把pictureView调整到看不见
                pictureView.snp.updateConstraints({ (make) in
                    make.top.equalTo(textLabel.snp.bottom)
                    make.size.equalTo(CGSize.zero).priority(999)
                })
            }
        }
    }
    
    /// 配图视图
    lazy var pictureView: WBStatusPictureView = WBStatusPictureView()
    
    /// 转发微博的文本lable
    lazy var textLabel: FFLabel = FFLabel(text: "test",fontSize: 15)
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.white
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}


extension WBRetweetedView {
    func setupUI () {
        //0. 背景颜色
        backgroundColor = UIColor(white: 0.9, alpha: 1.0)
        pictureView.backgroundColor = UIColor.yellow
        //1. 添加子控件
        addSubview(textLabel)
        addSubview(pictureView)

        //2. 自动布局
        textLabel.snp.makeConstraints { (make) in
            make.left.top.equalTo(self).offset(10)
            make.right.equalTo(self).offset(-10)
        }
        
        pictureView.snp.makeConstraints { (make) in
            make.left.equalTo(textLabel)
            //make.right.equalTo(textLabel)
            make.size.equalTo(CGSize.zero).priority(999)
            make.top.equalTo(textLabel.snp.bottom).offset(10)
            make.bottom.equalTo(self).offset(-10)
        }
    }
}
